package com.escape.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.escape.subject.common.enums.IsDeletedEnum;
import com.escape.subject.domain.convert.SubjectCategoryConvert;
import com.escape.subject.domain.entity.SubjectCategoryBO;
import com.escape.subject.domain.service.SubjectCategoryDomainService;
import com.escape.subject.infra.basic.entity.SubjectCategory;
import com.escape.subject.infra.basic.service.SubjectCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: escape_zebra
 * @CreateTime: 2024-12-18  12:12
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Service
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.add.BO: {}", JSON.toJSONString(subjectCategoryBO));
        }
        subjectCategoryBO.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategoryService.insert(subjectCategory);
    }

    /**
     * 查询分类大类
     * @return
     */
    @Override
    public List<SubjectCategoryBO> queryCategoryBO(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategoryBO.setIsDeleted(IsDeletedEnum.UN_DELETED.getCode());
        List<SubjectCategory> list = subjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBO> boList = SubjectCategoryConvert.INSTANCE.convertCategoryListToBoList(list);
        if (log.isInfoEnabled()) {
            log.info("SubjectCategoryDomainServiceImpl.queryCategoryBO.boList: {}", JSON.toJSONString(boList));
        }
        return boList;
    }

    /**
     * 更新分类
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public Boolean update(SubjectCategoryBO subjectCategoryBO) {
        if (log.isInfoEnabled()){
            log.info("SubjectCategoryDomainServiceImpl.update.BO: {}", JSON.toJSONString(subjectCategoryBO));
        }
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        int count = subjectCategoryService.update(subjectCategory);
        return count > 0;
    }

    /**
     * 删除分类
     * @param subjectCategoryBO
     * @return
     */
    @Override
    public Boolean delete(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryConvert.INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedEnum.DELETED.getCode());
        if (log.isInfoEnabled()){
            log.info("SubjectCategoryDomainServiceImpl.delete.BO: {}", JSON.toJSONString(subjectCategoryBO));
        }
        int count = subjectCategoryService.update(subjectCategory);
        return count > 0;
    }
}
